<?php
	
autoLoader();

$strAction = ( true == isset( $_POST['operation'] ) && true == valStr( $_POST['operation'] ) ) ? $_POST['operation'] : NULL;

switch( strtolower( trim( $strAction ) ) ) {
	case 'insert':
		handleInsertData();
		break;

	case 'update':
		handleUpdateData();
		break;

	case 'delete':
		handleDeleteData();
		break;

	case 'quick_insert':
		handleQuickInsertData();
		break;

	default:
		display( 'Provided action not found' );
		exit;
		break;
}
exit;

function handleInsertData() {
// 	display( $_REQUEST );
// 	display( $_POST ); exit;
	
	if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['patient_id'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=lab_case_paper&message=invalid&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	
	if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
		
		$strCheifComplaints 	= $_POST['cheif_complaints'];
		$strPrescription 		= $_POST['prescription'];
		$strDiagnosis 			= $_POST['diagnosis'];
		$strInvestigation		= $_POST['investigation'];
		$strRemark 				= $_POST['remark'];
		
		$strCheifComplaintsName = ( true == valStr( substr( $strCheifComplaints, 0, strpos( $strCheifComplaints, ';' ) ) ) ) ? substr( $strCheifComplaints, 0, strpos( $strCheifComplaints, ';' ) ) : substr( $strCheifComplaints, 0, 5 );
		$strPrescriptionName 	= ( true == valStr( substr( $strPrescription, 0, strpos( $strCheifComplaints, ';' ) ) ) ) ? substr( $strPrescription, 0, strpos( $strPrescription, ';' ) ) : substr( $strPrescription, 0, 5 );
		$strDiagnosisName 		= ( true == valStr( substr( $strDiagnosis, 0, strpos( $strDiagnosis, ';' ) ) ) ) ? substr( $strDiagnosis, 0, strpos( $strDiagnosis, ';' ) ) : strstr( $strDiagnosis, 0, 5 );
		$strInvestigationName 	= ( true == valStr( substr( $strInvestigation, 0, strpos( $strInvestigation, ';' ) ) ) ) ? substr( $strInvestigation, 0, strpos( $strInvestigation, ';' ) ) : substr( $strInvestigation, 0, 5 );
		$strRemarkName 			= ( true == valStr( substr( $strRemark, 0, strpos( $strRemark, ';' ) ) ) ) ? substr( $strRemark, 0, strpos( $strRemark, ';' ) ) : substr( $strRemark, 0, 5 );
		
		$boolIsValid = true;
		
		//Chief Complaints
		if( true == valStr( $strCheifComplaints ) ) {
			$objTemplate = new BaseTemplates();
			$objTemplate->setName( $strCheifComplaintsName );
			$objTemplate->setDescription( $strCheifComplaintsName );
			$objTemplate->setTemplateType( TemplateType::TEMPLATE_CHIEF_COMPLAINTS );
			$objTemplate->setCasePaperId( $_POST['case_paper_id'] );
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( htmlentities( $strCheifComplaints ) );
			$objTemplate->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setCreatedOn( 'now()' );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->insert() ) {
				$strMessage = '1. Chief Complaints inserted.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strPrescription ) ) {
			$objTemplate = new BaseTemplates();
			//Prescription
			$objTemplate->setName( $strPrescriptionName );
			$objTemplate->setDescription( $strPrescriptionName );
			$objTemplate->setTemplateType( TemplateType::TEMPLATE_PRESCRIPTION );
			$objTemplate->setCasePaperId( $_POST['case_paper_id'] );
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( htmlentities( $strPrescription ) );
			$objTemplate->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setCreatedOn( 'now()' );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->insert() ) {
				$strMessage = '2. Prescription inserted.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strDiagnosis ) ) {
			$objTemplate = new BaseTemplates();
			//Diagnosis
			$objTemplate->setName( $strDiagnosisName );
			$objTemplate->setDescription( $strDiagnosisName );
			$objTemplate->setTemplateType( TemplateType::TEMPLATE_DIAGNOSIS );
			$objTemplate->setCasePaperId( $_POST['case_paper_id'] );
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( htmlentities( $strDiagnosis ) );
			$objTemplate->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setCreatedOn( 'now()' );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->insert() ) {
				$strMessage = '3. Diagnosis inserted.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strInvestigation ) ) {
			$objTemplate = new BaseTemplates();
			//Investigation
			$objTemplate->setName( $strInvestigationName );
			$objTemplate->setDescription( $strInvestigationName );
			$objTemplate->setTemplateType( TemplateType::TEMPLATE_INVESTIGATION );
			$objTemplate->setCasePaperId( $_POST['case_paper_id'] );
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( htmlentities( $strInvestigation ) );
			$objTemplate->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setCreatedOn( 'now()' );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->insert() ) {
				$strMessage = '4. Diagnosis inserted.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strRemark ) ) {
			$objTemplate = new BaseTemplates();
			//Remark
			$objTemplate->setName( $strRemarkName );
			$objTemplate->setDescription( $strRemarkName );
			$objTemplate->setTemplateType( TemplateType::TEMPLATE_REMARK );
			$objTemplate->setCasePaperId( $_POST['case_paper_id'] );
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( htmlentities( $strRemark ) );
			$objTemplate->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setCreatedOn( 'now()' );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->insert() ) {
				$strMessage = '5. Remark inserted.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		$objCasePaper = new CasePapers();
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_POST['case_paper_id'] );
		
		if( true == valObj( $objCasePaper, 'CasePapers' ) ) {
			$objCasePaper->setFollowupDate( date( 'Y-m-d', strtotime( $_POST['followup_date'] ) ) );
			
			if( 1 == $objCasePaper->update() ) {
				$strMessage = '6. Remark inserted.';
			} else {
				$boolIsValid &= false;
			}
		}

		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=print_opd_case_paper&type=print&action=print&patient_id=' . $_POST['patient_id'] . '&case_paper_id='. $_POST['case_paper_id'] . '&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	return true;
}

function handleUpdateData() {
	
// 	display( $_REQUEST );
// 	display( $_POST ); exit;

	if( false == isset( $_POST['operation'] ) || false == valStr( $_POST['operation'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=opd_case_paper&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}

	$strCheifComplaints 	= $_POST['cheif_complaints'];
	$strPrescription 		= $_POST['prescription'];
	$strDiagnosis 			= $_POST['diagnosis'];
	$strInvestigation		= $_POST['investigation'];
	$strRemark 				= $_POST['remark'];

	//Chief Complaints
		if( true == valStr( $strCheifComplaints ) ) {
			$objTemplate = new Templates();
			$objTemplate = $objTemplate->fetchTemplatesById( $_POST['cheif_complaints_template_id'] );
			
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( $strCheifComplaints );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->update() ) {
				$strMessage = '1. Chief Complaints updated.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strPrescription ) ) {
			$objTemplate = new Templates();
			$objTemplate = $objTemplate->fetchTemplatesById( $_POST['prescription_template_id'] );
			
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( $strPrescription );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->update() ) {
				$strMessage = '2. Prescription updated.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strDiagnosis ) ) {
			$objTemplate = new Templates();
			$objTemplate = $objTemplate->fetchTemplatesById( $_POST['diagnosis_template_id'] );
			
			//Diagnosis
			$objTemplate->setData( $strDiagnosis );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->update() ) {
				$strMessage = '3. Diagnosis updated.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strInvestigation ) ) {
			$objTemplate = new Templates();
			$objTemplate = $objTemplate->fetchTemplatesById( $_POST['investigation_template_id'] );
			
			//Investigation
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( $strInvestigation );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->update() ) {
				$strMessage = '4. Diagnosis updated.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		if( true == valStr( $strRemark ) ) {
			$objTemplate = new Templates();
			$objTemplate = $objTemplate->fetchTemplatesById( $_POST['remark_template_id'] );
			
			//Remark
			$objTemplate->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setData( $strRemark );
			$objTemplate->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objTemplate->setUpdatedOn( 'now()' );
			
			if( 1 == $objTemplate->update() ) {
				$strMessage = '5. Remark updated.';
			} else {
				$boolIsValid &= false;
			}
		}
		
		$objCasePaper = new CasePapers();
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_POST['case_paper_id'] );
		
		if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
			$objCasePaper->setFollowupDate( date( 'Y-m-d', strtotime( $_POST['followup_date'] ) ) );
			
			if( 1 == $objCasePaper->update() ) {
				$strMessage = '6. case paper followed date updated.';
			} else {
				$boolIsValid &= false;
			}
		}

		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=print_opd_case_paper&type=print&action=print&patient_id=' . $_POST['patient_id'] . '&case_paper_id=' . $_POST['case_paper_id'] . '&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	
	
}

function handleInsertCasePaperData() {
}

?>